package com.tank.action.itemsObjct;

import java.io.File;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.tank.bean.Media;
import com.tank.bean.item.Image;
import com.tank.dao.ImageDao;
import com.tank.util.SaveImage;

public class ImageUploadAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private List<File> Filedata;
	/**
	 * 文件名称
	 */
	private List<String> FiledataFileName;
	private Image image;
	private String itemTag;
	
	@Autowired
	private ImageDao imageDao;
	
	@Override
	public String execute() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession seesion = request.getSession();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		Image image = null;
		SaveImage saveImage = new SaveImage();
		int index=0;
		String uuid="";
		String filedName="";
		long imageLength=0;
		String path="";
		Media media = null;
		JSONObject jsonObject = new JSONObject();
		if(Filedata!=null){
			for(int i=0;i<Filedata.size();i++){
				image=new Image();
				String newFilename = saveImage.makeNewFileName(FiledataFileName.get(i));
				String readlPath = ServletActionContext.getServletContext().getRealPath("\\imageTag");
				File newFile = new File(readlPath+"\\"+newFilename);
				
				index=newFilename.lastIndexOf('.');
                uuid=newFilename.substring(0,index);
				filedName=FiledataFileName.get(i);
				imageLength=Filedata.get(i).length();
				path=readlPath+"\\"+newFilename;
				media=new Media(uuid, filedName, imageLength, path);
				
				image = new Image();
				image.setName(newFilename);
				image.setRealName(filedName.substring(0,filedName.lastIndexOf('.')));
				image.setSize((int)imageLength);
				image.setItemTag(itemTag);
				image.setPubtime(new Date());
				try{
					imageDao.addIamge(image);
					saveImage.copyImage(Filedata.get(i),newFile);
					media.setStatus(1);
					jsonObject.element("result", true);
					jsonObject.element("media", media);
					out.println(jsonObject);
					out.flush();
					out.close();
				}catch (Exception e) {
					e.printStackTrace();
					media.setStatus(6);
					jsonObject.element("result", false);
//					jsonObject.element("errorMsg",e.getMessage());
					jsonObject.element("errorMsg", "出错了！");
					jsonObject.element("media", media);
					out.println(jsonObject);
					out.flush();
					out.close();
				}
				
			}
		}
		
		return "success";
		
	}

	public List<File> getFiledata() {
		return Filedata;
	}

	public void setFiledata(List<File> filedata) {
		Filedata = filedata;
	}

	public List<String> getFiledataFileName() {
		return FiledataFileName;
	}

	public void setFiledataFileName(List<String> filedataFileName) {
		FiledataFileName = filedataFileName;
	}

	public Image getImage() {
		return image;
	}

	public void setImage(Image image) {
		this.image = image;
	}

	public String getItemTag() {
		return itemTag;
	}

	public void setItemTag(String itemTag) {
		this.itemTag = itemTag;
	}

	public ImageDao getImageDao() {
		return imageDao;
	}

	public void setImageDao(ImageDao imageDao) {
		this.imageDao = imageDao;
	}
	
}
